package com.ccb.ic.bbs.controller;

import com.ccb.ic.bbs.cache.TagCache;
import com.ccb.ic.query.bbs.QuestionDTO;
import com.ccb.ic.query.bbs.ResultDTO;
import com.ccb.ic.bbs.exception.CustomizeErrorCode;
import com.ccb.ic.bbs.exception.CustomizeException;
import com.ccb.ic.bbs.provider.UserProvider;
import com.ccb.ic.bbs.service.QuestionService;
import com.ccb.ic.domain.bbs.Question;
import com.ccb.ic.domain.bbs.User;
import com.ccb.ic.domain.bbs.UserAccount;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class PublishController {
    @Value("${vaptcha.vid}")
    private String vaptcha_vid;
    @Autowired
    private QuestionService questionService;
    @Autowired
    UserProvider userProvider;

    @GetMapping("/bbs/p/publish/dict")
    public Object publish2(HttpServletRequest request) {
        User user = userProvider.getUser("token");
        Map<String, Object> model = new HashMap<>();
        if (user == null) {
            model.put("vaptcha_vid", vaptcha_vid);
            model.put("section", "login");
            model.put("sectionName", "登录");
            return ResultDTO.errorOf(model);
        }

        model.put("tags", TagCache.get());
        return ResultDTO.okOf(model);
    }

    @PostMapping("/bbs/p/publish")
    public Object doPublish2(
            @RequestBody Map<String, Object> params,
            HttpServletRequest request) {
        String title = (String) params.get("title");
        if (StringUtils.isEmpty(title)) {
            return ResultDTO.errorOf("标题不能为空");
        }
        String description = (String) params.get("description");
        String tag = (String) params.get("tag");
        if (StringUtils.isEmpty(tag)) {
            return ResultDTO.errorOf("tag不能为空");
        }
        Integer column2;
        Object column2_obj = params.get("column2");
        if (column2_obj != null && column2_obj instanceof Integer) {
            column2 = (Integer) column2_obj;
        } else {
            return ResultDTO.errorOf("column2类型错误");
        }

        Integer permission;
        Object permission_obj = params.get("permission");
        if (permission_obj != null && permission_obj instanceof Integer) {
            permission = (Integer) permission_obj;
        } else {
            return ResultDTO.errorOf("permission类型错误");
        }

        Long id = null;
        Object id_obj = params.get("id");
        if (permission_obj instanceof Long) {
            id = (Long) id_obj;
        }

        Map<String, Object> model = new HashMap<>();
        //  System.out.println("description:"+description);
        //   System.out.println("txtcontent:"+txtcontent);
        String defaultDescription = "<p id=\"descriptionP\"></p>";
        //String defaultDescription2= "<p id=\"descriptionP\" class=\"video\"></p>";
        //String defaultDescription3= "<p class=\"video\" id=\"descriptionP\"></p>";
        description = description.replaceAll("<p id=\"descriptionP\"></p>", ""); //剔出每次编辑产生的冗余p标签
        description = description.replaceAll("<p id=\"descriptionP\" class=\"video\"></p>", ""); //剔出每次的冗余p标签
        description = description.replaceAll("<p class=\"video\" id=\"descriptionP\"></p>", ""); //剔出每次的冗余p标签
        description = description.replaceAll("qs.niter", "qcdn.niter"); //剔出每次编辑产生的冗余p标签
        title = title.trim();
        tag = tag.trim();
        model.put("title", title);
        // model.addAttribute("description",description);
        model.put("tag", tag);
        model.put("tags", TagCache.get());
        User user = userProvider.getUser("token");
        UserAccount userAccount = userProvider.getUserAccount(user);
        //   System.out.println("permission:"+permission);
        // System.out.println("正文|"+description+"|"+"相同="+defaultDescription.equals(description));
        if (user == null) {
            model.put("vaptcha_vid", vaptcha_vid);
            model.put("section", "login");
            model.put("sectionName", "登录");
            return ResultDTO.errorOf(model);
        }

        if (StringUtils.isBlank(title)) {
            model.put("error", "标题不能为空");
            return ResultDTO.errorOf(model);
        }
        if (description == null || defaultDescription.equals(description)) {
            model.put("error", "问题补充不能为空");
            return ResultDTO.errorOf(model);
        }
        if (StringUtils.isBlank(tag)) {
            model.put("error", "标签不能为空");
            return ResultDTO.errorOf(model);
        }
//如果无需限制标签规范或者允许用户自定义标签，那么删掉下面这段代码就可以了
      /*  String invalid = TagCache.filterInvalid(tag);
        if (StringUtils.isNotBlank(invalid)) {
            model.addAttribute("error", "输入非法标签:" + invalid);
            return "p/publish";
        }*/

        Question question = new Question();
        question.setPermission(permission);
        question.setColumn2(column2);
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        question.setId(id);
        //question.setGmtCreate(new Date());
        //question.setGmtModified(question.getGmtCreate());
        // questionMapper.creat(question);
        questionService.createOrUpdate(question, userAccount);
        return ResultDTO.okOf(model);
    }

    @GetMapping("/bbs/p/publish/{id}")
    public Object edit2(@PathVariable(name = "id") Long id,
                        HttpServletRequest request) {
        QuestionDTO question = questionService.getById(id);
        User user = userProvider.getUser("token");
        if (question.getCreator() != user.getId()) {
            throw new CustomizeException(CustomizeErrorCode.CAN_NOT_EDIT_QUESTION);
        }
        Map<String, Object> model = new HashMap<>();
        model.put("title", question.getTitle());
        model.put("description", question.getDescription());
        model.put("column2", question.getColumn2());
        model.put("tag", question.getTag());
        model.put("id", question.getId());
        model.put("tags", TagCache.get());
        model.put("navtype", "publishnav");
        return ResultDTO.okOf(model);
    }


}
